package hz.mobileproject.pda.ui.activity.work;


import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.codbking.widget.DatePickDialog;
import com.codbking.widget.OnSureLisener;
import com.codbking.widget.bean.DateType;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.app.MainApp;
import hz.mobileproject.pda.entity.ContentSummaryIntentExtra;
import hz.mobileproject.pda.entity.request.AllTalk;
import hz.mobileproject.pda.entity.response.Rp_EduAllTalkItem;
import hz.mobileproject.pda.listener.CustomNetCallBack;
import hz.mobileproject.pda.network.Constance;
import hz.mobileproject.pda.ui.adapter.EduAllTalkAdapter;
import hz.mobileproject.pda.ui.base.BaseActivity;
import hz.mobileproject.pda.utils.HttpRqUtils;

/**
 * Created by zmybi on 2017/8/24.
 */

@ContentView(R.layout.activity_edu_alltalk)
public class EduAllTalkActivity extends BaseActivity implements EduAllTalkAdapter.OnItemClickListener, OnLoadmoreListener {
    @ViewInject(R.id.recyclerView_edu_alltalk)
    private RecyclerView mRecyclerView;
    @ViewInject(R.id.smartLayout)
    private SmartRefreshLayout mRefreshLayout;
    @ViewInject(R.id.bt_select_time_all)
    private Button bt_select_time_all;

    private EduAllTalkAdapter mAdapter;
    private Gson mGson;
    private int mPage = 1;
    private String mSelectedDate;

    @Override
    protected void initData() {
        mGson = new Gson();
        mSelectedDate = TimeUtils.getNowString(new SimpleDateFormat(Constance.DATE_YM));
        mAdapter = new EduAllTalkAdapter(this, new ArrayList<Rp_EduAllTalkItem>());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);
        mRefreshLayout.setOnLoadmoreListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPage = 1;
        queryAllTalkList();
    }

    @Event({R.id.bt_select_time_all})
    private void onViewClick(View v) {
        switch (v.getId()) {
            case R.id.bt_select_time_all:
                DatePickDialog dialog = new DatePickDialog(EduAllTalkActivity.this);
                //设置上下年分限制
                dialog.setYearLimt(5);
                //回显当前选择时间
                try {
                    dialog.setStartDate(new SimpleDateFormat(Constance.DATE_YM).parse(mSelectedDate));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                //设置标题
                dialog.setTitle("选择时间");
                //设置类型
                dialog.setType(DateType.TYPE_YM);
                //设置消息体的显示格式，日期格式
                dialog.setMessageFormat("yyyy-MM-dd HH:mm");
                //设置选择回调
                dialog.setOnChangeLisener(null);
                //设置点击确定按钮回调
                dialog.setOnSureLisener(new OnSureLisener() {
                    @Override
                    public void onSure(Date date) {
                        mPage = 1;
                        SimpleDateFormat sdf = new SimpleDateFormat(Constance.DATE_YM);
                        mSelectedDate = sdf.format(date);
                        LogUtils.e(mSelectedDate);    //1974-01
                        bt_select_time_all.setText(mSelectedDate);
                        //点击选择时间后 重新请求网络
                        queryAllTalkList();
                    }
                });
                dialog.show();
                break;
        }
    }


    /**
     * 列表点击事件
     * @param position
     * @param item
     */
    @Override
    public void OnItemClick(int position, Rp_EduAllTalkItem item) {
        Intent it = new Intent(EduAllTalkActivity.this,EduPlayMP3AndEvaluateActivity.class);
        ContentSummaryIntentExtra extra = new ContentSummaryIntentExtra();
        extra.setId(item.getRecord_id());
        extra.setType(ContentSummaryIntentExtra.UPDATE_EDU_TALK);
        extra.setPrisoner_id(item.getPrisoner_id());
        extra.setPrisoner_name(item.getPrisoner_name());
        extra.setTalk_effect(item.getTalk_effect());
        extra.setDanger_action(item.getDanger_action());
        extra.setDanger_evaluate(item.getDanger_evaluate());
        extra.setEmotion_evaluate(item.getEmotion_evaluate());
        extra.setFile_path(item.getAudio_path());

        it.putExtra(Constance.TO_PLAYMP3EVALUATE,extra);
        startActivity(it);
    }

    /**
     * 上拉加载更多
     * @param refreshlayout
     */
    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        queryAllTalkList();
    }


    /**
     * 加载列表
     */
    private void queryAllTalkList() {
        AllTalk rq = new AllTalk();
        rq.setCusNumber(MainApp.getInstance().getLoginUser().getCus_number() + "");
        rq.setQueryDept(MainApp.getInstance().getLoginUser().getDepartment_id() + "");
        rq.setQueryTime(mSelectedDate);
        rq.setQueryPage(mPage + "");
        HttpRqUtils.httpPost(Constance.BASE_URL + Constance.URL_EDUTALK_QUERY_ALL, rq, queryAllCallBack);
    }


    /**
     * 加载列表回调
     */
    private CustomNetCallBack queryAllCallBack = new CustomNetCallBack() {
        @Override
        public void onSuccessFromSer(int rtnCode, String rtnMsg, String data) {
            LogUtils.e(data);
            List<Rp_EduAllTalkItem> listdata = mGson.fromJson(data, new TypeToken<ArrayList<Rp_EduAllTalkItem>>() {
            }.getType());
            if (mRefreshLayout.isLoading()) {
                mRefreshLayout.finishLoadmore();
                mAdapter.addListData(listdata);
            } else {
                mAdapter.setListData(listdata);
            }
            if (listdata == null || listdata.size() <= 0) {
                ToastUtils.showLong("没有更多数据了");
                return;
            }
            mPage++;
        }

        @Override
        public void onErrorFromSer(int rtnCode, String rtnMsg) {
            ToastUtils.showLong("加载列表失败");
        }

        @Override
        public void onErrorFromNet(Throwable ex, boolean isOnCallback) {
            LogUtils.e(ex);
            ToastUtils.showLong("加载列表失败");
        }
    };

}
